# -*- coding: utf-8 -*-

from flask import Flask, url_for
from imooc import route_imooc
from common.libs.urlmanager import UrlManager
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine

app = Flask(__name__)
app.register_blueprint(route_imooc, url_prefix = "/imooc")

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:123456@127.0.0.1/mysql"
db = SQLAlchemy(app)

@app.route("/")
def hello_world():

    url = url_for("index")

    url_1 = UrlManager.buildurl("/api")

    msg = "hello world, url:%s, url1:%s" %(url, url_1)
    # app.logger.error(msg)
    # app.logger.info(msg)
    # app.logger.debug(msg)

    return msg

@app.route("/api")
def index():
    return "hello api"

@app.route("/api/hello")
def hello():
    from sqlalchemy import text
    sql = text("select * from `user`")
    result = db.engine.execute(sql)
    for row in result:
        app.logger.info( row )
    return "hello"

@app.errorhandler(404)
def page_not_found(error):
    app.logger.error(error)
    return "This page does not exist", 404

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)
